package com.adk.backend.controller;

import com.adk.backend.common.PageResult;
import com.adk.backend.common.Result;
import com.adk.backend.entity.LxActionLogs;
import com.adk.backend.mapper.ActionLogsMapper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import java.util.Date;

/**
 * 操作日志控制器
 */
@Slf4j
@RestController
@RequestMapping("/logs")
public class LogController {
    
    @Autowired
    private ActionLogsMapper actionLogsMapper;
    
    /**
     * 分页查询操作日志
     */
    @GetMapping("/list")
    public Result<PageResult<LxActionLogs>> getLogList(
            @RequestParam(required = false) Long userId,
            @RequestParam(required = false) String tableName,
            @RequestParam(required = false) String startDate,
            @RequestParam(required = false) String endDate,
            @RequestParam(defaultValue = "1") Integer pageNum,
            @RequestParam(defaultValue = "20") Integer pageSize) {
        
        Page<LxActionLogs> page = new Page<>(pageNum, pageSize);
        QueryWrapper<LxActionLogs> wrapper = new QueryWrapper<>();
        
        if (userId != null) {
            wrapper.eq("user_id", userId);
        }
        
        if (tableName != null && !tableName.isEmpty()) {
            wrapper.eq("table_name", tableName);
        }
        
        if (startDate != null && !startDate.isEmpty()) {
            wrapper.ge("add_time", startDate);
        }
        
        if (endDate != null && !endDate.isEmpty()) {
            wrapper.le("add_time", endDate);
        }
        
        wrapper.orderByDesc("add_time");
        
        Page<LxActionLogs> result = actionLogsMapper.selectPage(page, wrapper);
        PageResult<LxActionLogs> pageResult = PageResult.of(result.getTotal(), result.getRecords(), pageNum, pageSize);
        
        return Result.success(pageResult);
    }
}

